<!-- 转派规则 -->
<template>
    <div class="transferRulesDialog">
        <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" destroy-on-close v-model="visible" title=""
            :show-close="false" align-center class="dialogAdd">
            <div class="dialog_content">
                <div class="dialog_header">
                    <div class="title">
                        <img src="../../../../assets/images/rectangle07.png" alt="">
                        <span>发送通知公告</span>
                    </div>
                </div>
                <div class="dialog_body">
                    <div class="right">
                        <div class="headText">
                            工单流转过程中若关联执行人账号删除/禁用/缺失，则工单将会自动流转至转派负责人处，由转派负责人重新转派工单。默认转派负责人为平台超级管理员，指派新的转派负责人后将自动替换。
                        </div>
                        <div>
                            <span class="right_text"><span class="required">*</span>工单转派负责人</span>
                            <div>
                                <el-input v-model="departmentName" placeholder="工单转派负责人" :prefix-icon="Search" />
                            </div>
                           
                        </div>

                        <div class="platform_tree">
                            <el-scrollbar height="236px" always>
                                <el-tree ref="treeRef" :default-expanded-keys="expanded_keys" :props="defaultProps"
                                    node-key="id" :data="platformData.list" show-checkbox @check-change="handleCheckChange"
                                    :filter-node-method="filterNode" />
                            </el-scrollbar>
                        </div>
                    </div>
                </div>

                <div class="btn">
                    <el-button type="warning" @click="handleConfirm()">发 送</el-button>
                    <el-button @click="handleClose">取 消</el-button>
                </div>
            </div>

        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { noticePlatformAdd } from "@/http/message";
import { ElTree } from 'element-plus'
import { dptQueryDptNoPage } from "@/http/userManage"
import { Search } from '@element-plus/icons-vue'
const props = defineProps({
    dialogVisible: {
        type: Boolean,
        default: false
    }
})
let visible = ref(false);

const platformData = reactive({
    list: []
})

const expanded_keys: any = ref([]);//默认展开项


//emit(父组件中的自定义方法)
const emit = defineEmits(['closeDialog', 'confirmDialog', 'updateList']);
//关闭弹窗
const handleClose = () => {
    emit("closeDialog");
}

let departmentName = ref('');//项目名称


const filterNode = (value: any, data: any) => {
    if (!value) return true
    console.log('data', data);

    return data.name.includes(value)
}

interface InoticeUserList {
    dptId: string;
    dptName: string;
}


const ruleFormData = reactive({
    title: '',//标题
    content: '',//内容
    noticeUserList: [] as Array<InoticeUserList>,
    sendType: 1,//发送类型：1-立即发送，2-定时发送
    createTime: '',//定时发送时间
})

//监听打开是否打开弹窗
watch(() => props.dialogVisible, (newValue, oldValue) => {
    visible.value = newValue;
    //第一次打开时,请求发送对象数据
    if (newValue && platformData.list.length == 0) {
        getPlatformProject();
    }
});

//监听项目名称
watch(() => departmentName.value, (newValue) => {
    treeRef.value!.filter(newValue);//搜索发送对象
})


//点击确定按钮
const handleConfirm = () => {
  
}

const defaultProps = {
    children: 'children',
    label: 'name',
}

interface Tree {
    label: string
    children?: Tree[]
}
const treeRef = ref<InstanceType<typeof ElTree>>()

//勾选平台改变时
const handleCheckChange = (data: Tree) => {
    // console.log('data',data); parentPath

    // console.log('120',treeRef.value!.getCheckedKeys(false))
    // console.log('121', treeRef.value!.getCheckedNodes(true))
    let checkedNodes = treeRef.value!.getCheckedNodes(true);
    let noticeUserList = [] as Array<InoticeUserList>;
    if (checkedNodes && checkedNodes.length > 0) {
        checkedNodes.forEach(item => {
            noticeUserList.push({
                dptId: item.id,
                // dptName: item.name,
                dptName: getParentName(item.name, item.parentPath),
            })
        })
    }
    ruleFormData.noticeUserList = noticeUserList;
    console.log('ruleFormData', ruleFormData.noticeUserList);
}

const getParentName = (name: string, parentPath: string) => {
    // console.log('parentPath',parentPath);

    let name_all = "";
    if (parentPath) {
        let parentArr = parentPath.split(',');
        parentArr.forEach(item => {
            // console.log('item',item);
            let result = findElementById(platformData.list, item)
            // console.log('222result',result.name);
            if (name_all == "") {
                name_all += result.name;
            } else {
                name_all += "-" + result.name;
            }

        })
    }
    return name_all;
}

const findElementById: any = (arr: Array<any>, id: string) => {
    // console.log('230id',id);

    for (let i = 0; i < arr.length; i++) {
        const element = arr[i];
        if (element.id === id) {
            return element;
        }
        if (element.children && element.children.length > 0) {
            const foundElement = findElementById(element.children, id);
            if (foundElement) {
                return foundElement;
            }
        }
    }
    return null;
}

//获取平台部门列表
const getPlatformProject = () => {
    dptQueryDptNoPage({ noLoading: true }).then((res: any) => {
        if (res && res.result) {
            let list_new: any = [{
                id: '0',
                name: '智慧运维管理平台',
                children: []
            }];
            list_new[0].children = res.result;
            if (res.result.length > 0) {
                // expanded_keys.value = [res.result[0].id];
                expanded_keys.value = [list_new[0].id];
            }
            // console.log('list_new', list_new);
            // console.log('expanded_keys', expanded_keys.value);
            platformData.list = list_new;
            // console.log(' platformData.list', platformData.list);

        }
    }).catch(() => {
        platformData.list = [];
    })
}
// getPlatformProject();

</script>

<style scoped lang="scss">
.transferRulesDialog {
    :deep(.dialogAdd) {
        width: 480px;
        min-height: 550px;
        border: 1px solid rgba(192, 164, 91, 1);
        background: rgba(255, 255, 255, 1);
        border-radius: 2px;

        .el-dialog__header {
            padding: 0;

        }

        .el-dialog__body {
            padding: 0;

            .dialog_content {
                .dialog_header {
                    padding: 0;
                    height: 59px;
                    padding: 8px 32px 4px 62px;
                    line-height: 52px;
                    font-size: 16px;
                    font-weight: 400;
                    color: rgba(0, 0, 0, 1);
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    border-bottom: 1px dashed rgb(112, 105, 95);

                    // border-bottom: 2px dotted rgba(192, 164, 91, 1);
                    .title {
                        display: flex;
                        align-items: center;

                        img {
                            width: 4px;
                            height: 22px;
                            margin-right: 15px;
                        }

                    }

                    .close {
                        width: 25px;
                        height: 25px;
                        cursor: pointer;

                    }
                }

                .dialog_body {
                    display: flex;
                    padding: 28px 46px 0 46px;

                    .right {
                        .headText {
                            font-size: 12px;
                            margin-bottom: 10px;
                        }
                        .right_text {
                            font-size: 16px;
                            font-weight: 400;
                            letter-spacing: 0px;
                            line-height: 19px;
                            color: rgba(0, 0, 0, 1);
                            margin-bottom: 20px;
                            margin-top: 8px;

                            .required {
                                color: red;
                                vertical-align: middle;
                            }
                        }

                        .el-input {
                            width: 260px;
                            margin-top: 10px;

                            .el-input__wrapper {
                                border-radius: 2px;
                                border: 1px solid rgba(0, 0, 0, 0.15);
                                box-shadow: none;

                                &.is-focus {
                                    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(192, 164, 91, 0.8);
                                    border-color: rgba(192, 164, 91, 0.8);
                                }
                            }

                            .el-input-group__append {
                                width: 28px;
                                padding: 0;
                                border-radius: 0;
                                background-color: rgba(192, 164, 91, 0.3);
                                // background-color: #fff;
                                cursor: pointer;
                                box-shadow: none;
                                border: 0.5px solid rgba(192, 164, 91, 0.5);
                                border-left: 0;
                                display: flex;
                                justify-content: center;
                                align-items: center;
                            }
                        }

                        .platform_tree {
                            margin-top: 23px;
                            width: 395px;
                            height: 236px;
                            // background: rgba(112, 105, 95, 0.1);
                            padding: 0px 10px 0px 0px;

                            .el-tree {
                                background-color: transparent;
                                font-size: 14px;

                                .el-tree-node__content {
                                    height: 30px;
                                    line-height: 30px;

                                    &:hover {
                                        background-color: transparent;
                                    }

                                    .el-icon {
                                        font-size: 14px;
                                        color: rgba(192, 164, 91, 1);

                                        &.is-leaf {
                                            color: transparent;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                .btn {
                    margin-top: 20px;
                    text-align: right;
                    padding-right: 80px;

                    .el-button--info {
                        margin-left: 10px;
                    }
                }
            }
        }



    }
}
.el-tree {
    :deep(.el-icon) {
        svg {
            width: 12px;
            height: 12px;
            background: url("@/assets/images/images_new/arrow-down.png");
            background-size: 100% 100%;
        }
    }
}
</style>